#include "detectiondialog.h"
#include "ui_detectiondialog.h"
#include <QTableView>
#include <QFont>

DetectionDialog::DetectionDialog(SeatDetectionParameter &seatDetectionParameter, QWidget *parent)
    : QDialog(parent)
    , m_seatDetectionParameter(seatDetectionParameter)
    , ui(new Ui::DetectionDialog)
    , m_ppImageViewWidgets(Q_NULLPTR)
{
    ui->setupUi(this);

    Qt::WindowFlags windowFlag = Qt::Dialog;
    windowFlag |= Qt::WindowMinimizeButtonHint;
    windowFlag |= Qt::WindowMaximizeButtonHint;
    windowFlag |= Qt::WindowCloseButtonHint;
    setWindowFlags(windowFlag);

    number = 0;

    angle = QVector3D(-90.0, 0.0, 180.0);
    scale = QVector3D(1.0, 1.0, 1.0);
    pos = QVector3D(0.0, -0.5, 1.0);

    InitImageViewWidget();
    InitSeatViewWidget();
    InitInformationWidget();

    m_imgNoImage.load(":/res/res/image/noImage.jpg"); // 加载资源图像
}

DetectionDialog::~DetectionDialog()
{
    for (int i = 0; i < m_seatDetectionParameter.GetDetectionTotalSubImageCount(); i++) {
        delete m_ppImageViewWidgets[i];
    }
    delete m_ppImageViewWidgets;

    delete ui;
}

void DetectionDialog::InitImageViewWidget()
{
    m_ppImageViewWidgets
        = new ViewWidget *[m_seatDetectionParameter.GetDetectionTotalSubImageCount()];
    for (int i = 0; i < m_seatDetectionParameter.GetDetectionTotalSubImageCount(); i++) {
        int row = i / m_seatDetectionParameter.GetDetectionColumnSubImageCount();
        int col = i % m_seatDetectionParameter.GetDetectionColumnSubImageCount();
        m_ppImageViewWidgets[i] = new ViewWidget(this, true);
        m_ppImageViewWidgets[i]->SetPositionInfoShow(true);
        m_ppImageViewWidgets[i]->SetDefectInfoShow(true);
        m_ppImageViewWidgets[i]->SetFileNumber(i + 1); // 这个代表文件标号，从 1 开始
        QGridLayout *pLayout = (QGridLayout *) ui->groupBoxAllPosition->layout();

        pLayout->addWidget(m_ppImageViewWidgets[i], row, col);

        connect(m_ppImageViewWidgets[i],&ViewWidget::changeSelect,this,&DetectionDialog::HasChangeSelect);
    }
}

void DetectionDialog::InitSeatViewWidget()
{
    connect(this, &DetectionDialog::sendSeatModelControlMessage, ui->openGLWidget, &MyOpenGLWidget::controlMessage);
}

void DetectionDialog::InitInformationWidget(){
    ui->tabWidgetInformation->setTabText(0,"日志");
    ui->tabWidgetInformation->setTabText(1,"历史");

    /////////////////////////////////////////////////////////
    QStringList headerList;
    headerList.clear();
    headerList << QString::fromUtf8("座椅编号") << QString::fromUtf8("检测日期") << QString::fromUtf8("缺陷");

    // 假设你已经有一个QTableView对象叫做tableView

    // 创建一个QFont对象并设置你想要的字体属性
    QFont font = ui->tableWidgetHistory->font();
    font.setPointSize(12); // 设置字体大小为12
    font.setBold(true);    // 设置字体为粗体

    // 设置tableView的字体
    ui->tableWidgetHistory->setFont(font);

    ui->tableWidgetHistory->setColumnCount(headerList.size());
    ui->tableWidgetHistory->setHorizontalHeaderLabels(headerList);
    ui->tableWidgetHistory->horizontalHeader()->setFont(font);
    ui->tableWidgetHistory->horizontalHeader()->setStyleSheet(
        "QHeaderView::section{background:skyblue;color:black;}");
    ui->tableWidgetHistory->setStyleSheet("QTableWidget { border: 1px solid black; }");
    ui->tableWidgetHistory->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableWidgetHistory->setSelectionMode(QAbstractItemView::SingleSelection);
    QHeaderView* headerView = ui->tableWidgetHistory->verticalHeader();
    headerView->setHidden(true);

    ui->tableWidgetHistory->setColumnWidth(0, 150);
    ui->tableWidgetHistory->setColumnWidth(1, 200);
    ui->tableWidgetHistory->setColumnWidth(2, 75);

    QObject::connect(ui->tableWidgetHistory, &QTableWidget::itemSelectionChanged,
                     this, &DetectionDialog::handleItemSelectionChanged);
}

void DetectionDialog::handleItemSelectionChanged(){
    // 获取当前选中的项
    int currentRow = ui->tableWidgetHistory->currentRow();
    QTableWidgetItem *item = ui->tableWidgetHistory->item(currentRow, 0);
    if (item) {
        // 可以根据行号和列号进行操作
        QString seatNumber = item->text();
        emit sendQuerySeat(seatNumber);
    }
}

void DetectionDialog::ReceiveNewHistoryInforamtion(QList<HISTORYINFORMATION*> his){
    // 删除所有行
    while(ui->tableWidgetHistory->rowCount() > 0)
    {
        ui->tableWidgetHistory->removeRow(0);
    }

    int row = 0;
    foreach (HISTORYINFORMATION* pHI, his) {
        //从表格最后新增一行
        ui->tableWidgetHistory->insertRow(row);

        QTableWidgetItem *pSeatNumber = new QTableWidgetItem(pHI->seatNumber);
        ui->tableWidgetHistory->setItem(row, 0, pSeatNumber);

        QTableWidgetItem *pDetectionDate = new QTableWidgetItem(pHI->detectionDate);
        ui->tableWidgetHistory->setItem(row, 1, pDetectionDate);

        QTableWidgetItem *pDefectCount = new QTableWidgetItem(QString("%1").arg(pHI->defectCount));
        ui->tableWidgetHistory->setItem(row, 2, pDefectCount);

        row++;
    }
}

/**
 * @brief DetectionDialog::SetDetectionResult
 * @param seatDetectionParameter => 系统主配置参数
 * @param detectionResult => 一次识别结果
 * @param isShow10X => true:显示原始尺寸的 1/10，false:显示原始尺寸
 */
void DetectionDialog::SetDetectionResult(SeatDetectionParameter &seatDetectionParameter,
                                         DetectionResult &detectionResult,bool isShow10X)
{
    for (int i = 0; i < m_seatDetectionParameter.GetDetectionTotalSubImageCount(); i++) {
        m_ppImageViewWidgets[i]->SetDetectionResult(seatDetectionParameter, detectionResult,isShow10X);
    }
    // 获取图像编号
    int nFileName = detectionResult.GetInfo().GetFileName().toInt();
    // 显示座椅3D模型的不同部位
    QList<SEATSHOWINFO> ssies = m_seatDetectionParameter.GetSeatShowInfos();
    SEATSHOWINFO ssi = ssies.at(nFileName);
    emit sendSeatModelControlMessage(ssi.angle, ssi.scale, ssi.viewpos);
}

void DetectionDialog::HasChangeSelect(int index){
    QList<SEATSHOWINFO> ssies = m_seatDetectionParameter.GetSeatShowInfos();
    SEATSHOWINFO ssi = ssies.at(index);
    emit sendSeatModelControlMessage(ssi.angle, ssi.scale, ssi.viewpos);
}

/**
 * @brief DetectionDialog::on_pushButtonSwitchImage_clicked
 * 图片切换
 */
void DetectionDialog::on_pushButtonSwitchImage_clicked()
{
    number %= m_seatDetectionParameter.GetSeatShowInfos().size();

    QList<SEATSHOWINFO> ssies = m_seatDetectionParameter.GetSeatShowInfos();

    SEATSHOWINFO ssi = ssies.at(number);

    emit sendSeatModelControlMessage(ssi.angle, ssi.scale, ssi.viewpos);

    number++;
}
/**
 * @brief DetectionDialog::on_pushButtonReset_clicked
 * 复位
 */
void DetectionDialog::on_pushButtonReset_clicked(){
    QList<SEATSHOWINFO> ssies = m_seatDetectionParameter.GetSeatShowInfos();
    SEATSHOWINFO ssi = ssies.at(0);
    emit sendSeatModelControlMessage(ssi.angle, ssi.scale, ssi.viewpos);
}
/**
 * @brief DetectionDialog::on_pushButtonDetectionExit_clicked
 * 退出检测窗口，回退主窗口
 */
void DetectionDialog::on_pushButtonDetectionExit_clicked()
{
    this->close();
    emit sendButtonControlCmd("CloseDialog");
}
/**
 * @brief DetectionDialog::on_pushButtonDeviationRelease_clicked
 * 偏差放行
 */
void DetectionDialog::on_pushButtonDeviationRelease_clicked()
{
    emit sendButtonControlCmd("DeviationRelease");
}
/**
 * @brief DetectionDialog::on_pushButtonIsolate_clicked
 * 隔离
 */
void DetectionDialog::on_pushButtonIsolate_clicked()
{
    emit sendButtonControlCmd("Isolate");
}
/**
 * @brief DetectionDialog::on_pushButtonOnlineMaintenance_clicked
 * 在线维修
 */
void DetectionDialog::on_pushButtonOnlineMaintenance_clicked()
{
    emit sendButtonControlCmd("OnlineMaintenance");
    for(int i = 0;i  < 10000;i++){
        ui->textBrowserLog->append(QString("%1").arg(i));
        ui->textBrowserLog->moveCursor(QTextCursor::End);
    }
}

void DetectionDialog::ReceiveLogMessage(QString message){
    ui->textBrowserLog->append(message);
    ui->textBrowserLog->moveCursor(QTextCursor::End);
}

/**
 * @brief DetectionDialog::ClearImage
 * 使用默认的图片，表示当前没有图像
 */

void DetectionDialog::ClearImage(){
    // 恢复座椅默认姿态
    on_pushButtonReset_clicked();
    // 清除上一个座椅的结果

    for (int i = 0; i < m_seatDetectionParameter.GetDetectionTotalSubImageCount(); i++) {
        m_ppImageViewWidgets[i] ->SetImage(m_imgNoImage);
        m_ppImageViewWidgets[i]->ClearDefectInfo();
        m_ppImageViewWidgets[i]->ClearPosInfo();
    }
}

// 重载关闭事件
void DetectionDialog::closeEvent(QCloseEvent *event) {
    emit sendButtonControlCmd("CloseDialog");
    event->accept();
}
